package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.MaterialRequisition;
import com.woniuxy.factory.entity.WorkOrder;
import com.woniuxy.factory.mapper.ApplicationRecordMapper;
import com.woniuxy.factory.mapper.MaterielAppMapper;
import com.woniuxy.factory.service.MaterielAppService;
import com.woniuxy.factory.util.ApplicationType;
import com.woniuxy.factory.util.AuditType;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MaterielAppServiceImp implements MaterielAppService {

    //申请接口
    @Autowired(required = false)
    MaterielAppMapper mam;
    //申请记录表接口
    @Autowired(required = false)
    ApplicationRecordMapper arm;

    //查询申请信息
    @Override
    public List<MaterialRequisition> selectApplyInfo(MaterialRequisition materialRequisition,int page, int limit) {
        //设置初始页
        page = (page-1)*limit;
        List<MaterialRequisition> lmr = mam.selectApplyInfo(materialRequisition,page,limit);
        //遍历判断申请的状态
        for (MaterialRequisition m : lmr){
           if(m.getApplicationRecord().getApplystatus() == AuditType.ALREADY_PASSED){
               m.setStateInfo("已同意");

           }else if( m.getApplicationRecord().getApplystatus()== AuditType.NO_PASS){
               m.setStateInfo("未通过审核");
           }else{
               m.setStateInfo("未审核");
           }
        }
        return lmr;
    }

    //查询申请信息总的数量
    @Override
    public int selectApplyInfoSum(MaterialRequisition materialRequisition) {
        int count = mam.selectApplyInfoSum(materialRequisition);
        return count;
    }

    //添加申请信息
    //事物控制
    @Transactional
    @Override
    public void addApply(MaterialRequisition mr) {
        //定义申请类型编号
        int applicationType = ApplicationType.MATERIAL_REQUISITION;
        //自动生成申请编号
        String matId = UuidUtil.get16UUID();
        //给申请实体类申请编号id属性赋值
        mr.setMatId(matId);
        //自动生成申请记录表编号
        String aprId = UuidUtil.get16UUID();
       //将申请记录表编号，申请编号，申请类型编号插入申请记录表
        mam.insertApplicationrecord(matId,applicationType,aprId);
        //将申请信息插入申请表
        mam.addApply(mr);
    }

    //申请信息回显
    @Override
    public MaterialRequisition selectApplyByid(String matId) {
        MaterialRequisition lmr = mam.selectApplyByid(matId);
        return lmr;
    }

    //修改申请信息
    @Override
    public void updateApply(MaterialRequisition mr) {
        mam.updateApply(mr);
    }

    //删除申请信息
    @Override
    public void deleteApply(String matId) {
        mam.deleteApply(matId);
    }


}
